package com.xyzh05.webapp.action;

import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.User;
import com.xyzh05.model.UserInfo;
import com.xyzh05.model.VipService;
import com.xyzh05.service.UserInfoManager;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;

public class UserInfoAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -6237347936486211446L;
	
	private UserInfoManager userInfoManager;
	private VipServiceManager vipServiceManager;
	private List<UserInfo> userInfoList;
	private UserInfo userInfo;
	private VipService vipService;
	private User user;
	private Long id;
	private String query;
    private PageBean pageBean;
	private Integer resultCode;
	private Long vipServiceId;
    private String major;
    private String nickname;


	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
	}

	public String list() {    	
    	pageBean = userManager.queryOrdinaryAdviser(pageBean,major,nickname);
		return SUCCESS;
	}

	public String delete() {
		userInfoManager.remove(userInfo.getId());
		saveMessage(getText("userInfo.deleted"));

		return SUCCESS;
	}

	/**
	 * 修改个人信息
	 * 
	 * @return
	 */
	public String edit() {
		if (id != null) {
			user = userManager.get(id);
			userInfo = user.getUserInfo();
		} else {
			userInfo = new UserInfo();
		}
		return SUCCESS;
	}

	/**
	 * 保存或者修改个人信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			User u = userManager.get(user.getId());
			u.setEmail(user.getEmail());
			
			UserInfo ui = u.getUserInfo() == null ? new UserInfo() : u.getUserInfo();			
			ui.setUpdateUser(getCurrentUser().getId());
			ui.setUser(u);
			ui.setUndergraduateSchool(userInfo.getUndergraduateSchool());
			ui.setUndergraduateMajor(userInfo.getUndergraduateMajor());
			ui.setApplyMajor(userInfo.getApplyMajor());
			ui.setAdmissionYear(userInfo.getAdmissionYear());
			ui.setToefl(userInfo.getToefl());
			ui.setGre(userInfo.getGre());
			ui.setUndergraduateGpa(userInfo.getUndergraduateGpa());
			ui.setQq(userInfo.getQq());
			ui.setMobilephone(userInfo.getMobilephone());
			if(u.getUserInfo() == null){
				u.setUserInfo(ui);
			}
			userManager.saveOrUpdate(u);
    		resultCode = 1;
		} catch (Exception e) {
			resultCode = 0;
			log.warn(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 显示VIP信息 showQuery or showModify
	 * 
	 * @return
	 */
	public String doshowInfo() {
		if (vipServiceId != null) {
			vipService = vipServiceManager.get(vipServiceId);
			user = userManager.get(vipService.getVip().getId());
			userInfo = user.getUserInfo();
		} else {
			userInfo = new UserInfo();
		}
		return SUCCESS;
	}
	
	public String searchUserList(){
    	pageBean = userManager.showEditors(pageBean);
    	return SUCCESS;
	}
    
    public String doShowOrdinaryAdviser(){    	
    	pageBean = userManager.queryOrdinaryAdviser(pageBean,major,nickname);    	
    	return SUCCESS;
    }
	
	
	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}

	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}

	public List<UserInfo> getUserInfoList() {
		return userInfoList;
	}

	public void setUserInfoList(List<UserInfo> userInfoList) {
		this.userInfoList = userInfoList;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public VipService getVipService() {
		return vipService;
	}

	public void setVipService(VipService vipService) {
		this.vipService = vipService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

}